#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "sqlite3.h"
#include "logger.h"
#include "database.h"

int database_open(sqlite3 **db, char *filename)
{

    char                create[128];
	char               *errmsg;
	char	    	  **dbresult;
	int		            row=0;
	int		            column=0;

	memset(create, 0, sizeof(create));
    snprintf( create, sizeof(create), "create table if not exists mqttpub_pj(ID integer PRIMARY KEY, SN char(10), TIME char(50), TEMP char(10));");
	if(sqlite3_open(filename, db)!=0)
	{
		log_error("open()%s\n",errmsg);
	}


    if( sqlite3_exec(*db, create, NULL, NULL, &errmsg)!=0)
	{
		log_error("create()%s\n",errmsg);
		sqlite3_close(*db);
	}
	return 0;
}


int database_query_data(sqlite3 *db, char  *buff)//从数据库中获取一条数据
{
	char               *errmsg;
	char		      **dbresult;
	int		            row=0;
	int		            column=0;
	char                select[128];

	char               *p=buff;

	memset(select, 0, sizeof(select));
	snprintf(select, sizeof(select),"select * from mqttpub_pj limit 1;");

    if(sqlite3_get_table(db, select, &dbresult, &row, &column, &errmsg)!=SQLITE_OK)//为什么传的是db的值不是地址???
	{
		log_error("select()%s\n", errmsg);
    }
	
	
//	printf("%s/%s/%s\n", dbresult[5], dbresult[6], dbresult[7]);
	sprintf(p, "%s/%s/%s", dbresult[5], dbresult[6], dbresult[7]);
//	printf("data: %s\n", buff);
//	printf("%s/%s/%s\n", buff->SN, buff->TIME, buff->TEMP);
	return 0;
}

int database_delete_data(sqlite3 *db)//删除数据库
{
    char               *errmsg;
	char                delete[128];
	char		  **dbresult;
	int		    row=0;
	int		    column=0;


	memset(delete, 0, sizeof(delete));
	snprintf(delete, sizeof(delete),"delete from mqttpub_pj where rowid in ( select rowid from mqttpub_pj LIMIT 1);");
    if(sqlite3_exec(db, delete, NULL, NULL, &errmsg)!=0)//为什么传的是db的值不是地址???
	{
        log_info("delete()%s\n", errmsg);
	    sqlite3_close(db);

	}
	log_info("Succeeded in deleting a piece of data\n");
	return 0;
}
int database_get_id( sqlite3 *db)//存入数据库	
{
	
	char          get_id[128];
	char         *errmsg;
	char	    **dbresult;
	int           row=0;
	int 	      column=0;
	int           rv;

	if( !db)
	{
		log_error("ERROR: Invalid input arguments\n");//无效的参数
		return -1;
	}

	memset(get_id, 0, sizeof(get_id));
	snprintf(get_id, sizeof(get_id), "select count(*) from mqttpub_pj;"); 
	if(sqlite3_get_table(db, get_id, &dbresult, &row, &column, &errmsg)!=SQLITE_OK)//为什么传的是db的值不是地址???
	{
            log_error("delete()%s\n", errmsg);
	}

	rv=atoi(dbresult[1]);
	log_info(" %d group of data\n", rv);
	return  rv;
}
int database_save_server( sqlite3 *db, char *data_buff)	
{
	
	char          delim[] = "/";
	char         *p = NULL;	
	int           i=0 ;
	char         *data[2];
	char          insert[1024];
	char        **dbresult;
    char         *errmsg;
	int           row=0;
	int           column=0;
	float         temp_data;

	for(p = strtok(data_buff, delim);p != NULL; p = strtok(NULL, delim))
	{
		data[i]=p;
		i++;
	}

	temp_data=atof(data[2]);
	snprintf(insert, sizeof(insert), "insert into mqttpub_pj(SN, TIME, TEMP) values('%s', '%s', '%2.2f');", data[0], data[1], temp_data); 


	if(sqlite3_exec(db, insert, NULL, NULL, &errmsg) != 0)
	{
	    	printf("insert():%s\n", errmsg);
		return -1;
	}

	return 0;
}

